package com.example.day10_zuoye.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioGroup;

import com.example.day10_zuoye.R;
import com.example.day10_zuoye.adapter.MyFragmentPagerAdapter;
import com.example.day10_zuoye.fragmnet.WelComeOneFragment;
import com.example.day10_zuoye.fragmnet.WelComeThreeFragment;
import com.example.day10_zuoye.fragmnet.WelComeTwoFragment;
import com.example.day10_zuoye.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

public class WelcomeActivity extends AppCompatActivity {

    private ViewPager view_pager;
    private RadioGroup group;
    private List<Fragment> list;
    private MyFragmentPagerAdapter myFragmentPagerAdapter;

    private Timer timer;
    private int position = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        String go = SPUtils.getInstance(this).getString("go");
        if(!go.equals("")){//之前走过引导页
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            return;//
        }


        initView();
        initTimer();
    }

    private void initTimer() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
               runOnUiThread(new Runnable() {//在UI线程，不用handler
                   @Override
                   public void run() {
                       view_pager.setCurrentItem(position++);
                       if(position == 3){
                           timer.cancel();
                       }
                   }
               });


            }
        },1000,1000);
    }

    private void initView() {
        view_pager = (ViewPager) findViewById(R.id.view_pager);
        group = (RadioGroup) findViewById(R.id.group);
        list = new ArrayList<>();
        list.add(new WelComeOneFragment());
        list.add(new WelComeTwoFragment());
        list.add(new WelComeThreeFragment());
        myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),list);
        view_pager.setAdapter(myFragmentPagerAdapter);

        view_pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        group.check(R.id.rb1);
                        break;
                    case 1:
                        group.check(R.id.rb2);
                        break;
                    case 2:
                        group.check(R.id.rb3);
                        break;
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
}
